﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MHYS.DEMO.Model;
using System.Data.SqlClient;
namespace MHYS.DEMO.DAL
{
    public class OrderService
    {

        /// <summary>
        /// 插入订单项
        /// </summary>
        /// <param name="order"></param>
        /// <returns>插入是否成功</returns>
        public static bool InsertOrder(Order order)
        {
            try
            {
                //查询刚插入订单的订单号
                SqlParameter[] paras = new SqlParameter[] {
                        new SqlParameter("@UserId",order.UserID),
                        new SqlParameter("@OrderTime",order.OrderTime),
                        new SqlParameter("@OrderMoney",order.OrderMoney),
                        new SqlParameter()
                };
                SqlParameter sqlParameter = new SqlParameter("@Orderid", System.Data.SqlDbType.Int);
                sqlParameter.Direction = System.Data.ParameterDirection.Output;//指明参数是输出参数
                paras[3] = sqlParameter;
                int orderID = DBHelper.ExecuteProdure("OrderID", paras);

                //根据查询的OrderID循环插入订单项
                foreach (OrderItem item in order.OrderItem)
                {
                    string sql = string.Format("INSERT INTO OrderItem Values('{0}','{1}','{2}','{3}')", item.Product.Gid,item.PayMoney, item.Num, orderID);
                    DBHelper.ExecNonQuery(sql);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


    }
}
